package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.CardDao;
import com.example.child_protection.dao.ChildArchivesDao;
import com.example.child_protection.dao.ChildArchivesValuesDao;
import com.example.child_protection.dao.ChildOtherInfoDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.ChildArchivesValuesDTO;
import com.example.child_protection.dto.info.ChildOtherInfoDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.ChildArchivesInsertRequestDTO;
import com.example.child_protection.dto.request.add.ChildArchivesParentSaveRequestDTO;
import com.example.child_protection.dto.request.add.ChildArchivesTurnSaveRequestDTO;
import com.example.child_protection.dto.request.add.ChildArchivesValuesInsertRequestDTO;
import com.example.child_protection.dto.request.update.ChildArchivesUpdateRequestDTO;
import com.example.child_protection.enums.CardStatesEnums;
import com.example.child_protection.enums.CloseCaseTypeEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.enums.TurnTypeEnums;
import com.example.child_protection.model.CardModel;
import com.example.child_protection.model.ChildArchivesModel;
import com.example.child_protection.model.ChildArchivesValuesModel;
import com.example.child_protection.model.ChildOtherInfoModel;
import com.example.child_protection.service.ChildArchivesManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 档案管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class ChildArchivesManageServiceImpl extends MyBaseService implements ChildArchivesManageService {

    @Autowired
    public ChildArchivesDao childArchivesDao;

    @Autowired
    public CardDao cardDao;

    @Autowired
    public ChildArchivesValuesDao childArchivesValuesDao;

    @Autowired
    public ChildOtherInfoDao childOtherInfoDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(ChildArchivesInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_ARCHIVES_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildArchivesInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildArchivesInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildArchivesModel model = new ChildArchivesModel();
                BeanUtils.copyProperties(requestDTO,model);

                CardModel cardModel = cardDao.findByCardNo(model.getCardNo());
                //查看当前卡号的状态是否是准备激活
                if(!cardModel.getStates().equals(CardStatesEnums.READY_TO_ACTIVE)){
                    cardModel.setStates(CardStatesEnums.ACTIVE);
                    //把状态修改为已激活
                    cardDao.updateSelective(cardModel);
                }else{
                    throw new Exception("儿保卡状态异常，请重新获取儿保卡号！");
                }
                childArchivesDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 保存儿童基本信息
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO saveValues(ChildArchivesValuesInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_VALUE_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildArchivesValuesInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildArchivesValuesInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();

                //删除以前的档案数据
                ChildArchivesValuesModel childArchivesValuesModel = new ChildArchivesValuesModel();
                childArchivesValuesModel.setArchivesId(requestDTO.getArchivesId());
                childArchivesValuesModel.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                childArchivesValuesDao.deleteByArchivesId(childArchivesValuesModel);

                //保存档案数据
                List<ChildArchivesValuesModel> infoValues = JSONObject.parseArray(requestDTO.getValues(), ChildArchivesValuesModel.class);
                for(ChildArchivesValuesModel c :infoValues){
                    c.setArchivesId(requestDTO.getArchivesId());
                    c.setCreatePersonId(requestDTO.getCreatePersonId());
                    c.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    childArchivesValuesDao.insert(c);
                }
                //修改基本信息
                ChildArchivesModel ca = new ChildArchivesModel();
                BeanUtils.copyProperties(requestDTO,ca);
                ca.setId(requestDTO.getArchivesId());
                ca.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                childArchivesDao.updateSelective(ca);
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 保存儿童父母信息
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO saveParentValues(ChildArchivesParentSaveRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_PARENT_VALUES_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildArchivesParentSaveRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildArchivesParentSaveRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildOtherInfoModel childOtherInfoModel = childOtherInfoDao.findByArchivesId(requestDTO.getArchivesId());
                ChildOtherInfoModel model = new ChildOtherInfoModel();
                BeanUtils.copyProperties(requestDTO, model);
                if(null!=childOtherInfoModel) {
                    model.setId(childOtherInfoModel.getId());
                    model.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    childOtherInfoDao.updateSelective(model);
                }else{
                    model.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                    model.setTurnOutStates(TurnTypeEnums.NO);
                    model.setCloseCaseStates(CloseCaseTypeEnums.NO);
                    childOtherInfoDao.insert(model);
                }
                //修改妈妈的电话
                ChildArchivesModel ca = new ChildArchivesModel();
                ca.setId(requestDTO.getArchivesId());
                ca.setMotherPhone(requestDTO.getMotherPhone());
                ca.setLastUpdatePersonId(requestDTO.getCreatePersonId());
                childArchivesDao.updateSelective(ca);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 保存儿童转出与结案信息
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO saveTurnInfo(ChildArchivesTurnSaveRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_TURN_INFO_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildArchivesTurnSaveRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildArchivesTurnSaveRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildOtherInfoModel childOtherInfoModel = childOtherInfoDao.findByArchivesId(requestDTO.getArchivesId());
                ChildOtherInfoModel model = new ChildOtherInfoModel();
                BeanUtils.copyProperties(requestDTO, model);
                if(null!=childOtherInfoModel) {
                    model.setId(childOtherInfoModel.getId());
                    childOtherInfoDao.updateSelective(model);
                }else{
                    childOtherInfoDao.insert(model);
                }
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }
    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(ChildArchivesUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.CHILD_ARCHIVES_UPDATE,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<ChildArchivesUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(ChildArchivesUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildArchivesModel model = new ChildArchivesModel();
                BeanUtils.copyProperties(requestDTO,model);
                childArchivesDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service (ServiceActionEnums.CHILD_ARCHIVES_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                ChildArchivesModel model = new ChildArchivesModel();
                BeanUtils.copyProperties(requestDTO,model);
                childArchivesDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

}
